package com.yg.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yg.config.FilePathConfig;
import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.TeacherEducationPromotionTemplate;
import com.yg.entity.singleEntity.TeacherEducationPromotion;
import com.yg.listener.TeacherEducationPromotionReadListener;
import com.yg.mapper.TeacherEducationPromotionMapper;
import com.yg.service.TeacherEducationPromotionService;
import com.yg.service.TeacherEducationService;
import com.yg.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 对应sheet02，此表为教师学历提升表
 * 注意！此表的id和教师的id是一个id，同时教师在进行学历提升后，要更改教师的最高学历，即更改教师学历表 服务实现类
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@Service
public class TeacherEducationPromotionServiceImpl extends ServiceImpl<TeacherEducationPromotionMapper, TeacherEducationPromotion> implements TeacherEducationPromotionService {

    @Autowired
    private FilePathConfig config;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TeacherEducationService teacherEducationService;

    /**
     * 条件分页带查询
     *
     * @param current
     * @param limit
     * @param teacherEducationPromotion 教师学历提升的条件
     * @param vip
     * @return
     */
    @Override
    public R getTeacherPromotion(long current, long limit, TeacherEducationPromotionTemplate teacherEducationPromotion, String vip) {
        try {
            Long total;
            List<TeacherEducationPromotionTemplate> list = null;
            if (vip.equals("v1")) {
                list = baseMapper.getTeacherPromotion(current, limit, teacherEducationPromotion);
                total = baseMapper.getTeacherPromotionPages(teacherEducationPromotion);
            } else {
                list = baseMapper.getTeacherPromotionById(current, limit, teacherEducationPromotion);
                total = baseMapper.getTeacherPromotionByIdPages(teacherEducationPromotion);
            }
            Long pages = (total / limit) + 1;
            return R.OK().data("teacherEducationPromotionList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR();
        }
    }

    /**
     * 从表格中读取数据导入到数据库中
     *
     * @param file 表格数据
     * @param r
     * @return
     */
    @Transactional
    @Override
    public void readExcel(MultipartFile file, R r) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, TeacherEducationPromotionTemplate.class, new TeacherEducationPromotionReadListener(this, teacherService, teacherEducationService, r)).headRowNumber(3).sheet().doRead();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 在数据库中读取数据写入到表格中
     *
     * @param promotionTemplate
     * @param response
     */
    @Transactional
    @Override
    public void writeExcel(TeacherEducationPromotionTemplate promotionTemplate, HttpServletResponse response) {
        List<TeacherEducationPromotionTemplate> teacherEducationPromotions = baseMapper.writeExcel(promotionTemplate);

        //模板文件
        String fileTemplate = config.getWriteExcelFilePath() + "sheet02Template.xlsx";
        String file = "E:\\sheet02Write.xlsx";

        //导出(匿名内部类SheetWriteHandler)
        response.setHeader("Content-disposition", "attachment;filename=" + "sheet02Write.xlsx");
        try {
            EasyExcel.write(response.getOutputStream()).withTemplate(fileTemplate).registerWriteHandler(new SheetWriteHandler() {

                @Override
                public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

                }

                @Override
                public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
                    //日志
                }


            }).sheet().doFill(teacherEducationPromotions);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}